package com.keyinong.jiashiyuan.view;

import java.util.ArrayList;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.keyinong.jiashiyuan.R;
import com.keyinong.jiashiyuan.adapter.XiangqingAdapter;
import com.keyinong.jiashiyuan.entity.XiangqingEntity;
import com.keyinong.jiashiyuan.tools.HttpTools;
import com.loopj.android.http.JsonHttpResponseHandler;

public class DetaileActivity extends Activity {
	private XiangqingAdapter xiangqingadapter;
	int geshu;
	ListView lv;
	TextView tv_shangpin;
	TextView tv_shuliang;
	TextView tv_total;
	TextView tv_xiadan;
	TextView tv_songhuo;
	ImageButton ib_fanhui;

	/**获得数据*/
	ArrayList<XiangqingEntity> infolist;
	HttpTools httptools=null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_detaile);
		lv=(ListView)findViewById(R.id.lv_quanbu);
		tv_shangpin=(TextView)findViewById(R.id.tv_shangpin);
		tv_shuliang=(TextView)findViewById(R.id.tv_shuliang);
		tv_total=(TextView)findViewById(R.id.tv_total);
		tv_xiadan=(TextView)findViewById(R.id.tv_xiadan);
		tv_songhuo=(TextView)findViewById(R.id.tv_songhuo);
		ib_fanhui=(ImageButton)findViewById(R.id.img1);
		getShouhuo();
		getShangpin();
		ib_fanhui.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(getApplicationContext(), JiashiyuanActivity.class);
				startActivity(intent);
			}
		});
	}

	private void getShouhuo() {
		infolist=new ArrayList<XiangqingEntity>();
		SharedPreferences sp = getSharedPreferences("spReg",
				Context.MODE_PRIVATE);
		String token = sp.getString("token", "");
		String id = getIntent().getStringExtra("id");
//		Log.i("zq", id);
//		Log.e("zq", "token1---" + token);
		if (httptools == null) {
			httptools = new HttpTools(getApplicationContext());
		}
		httptools.Shangpin(token, id, new JsonShouhuo());
		
	}

	private void getShangpin(){
		infolist=new ArrayList<XiangqingEntity>();
		SharedPreferences sp = getSharedPreferences("spReg",
				Context.MODE_PRIVATE);
		String token = sp.getString("token", "");
		String id = getIntent().getStringExtra("id");
//		Log.i("zq", id);
//		Log.e("zq", "token1---" + token);
		if (httptools == null) {
			httptools = new HttpTools(getApplicationContext());
		}
		httptools.Shangpin(token, id, new JsonShangpin());
	}
	
	public class JsonShouhuo extends JsonHttpResponseHandler {
		@Override
		public void onSuccess(int statusCode, Header[] headers,
				JSONObject response) {
			super.onSuccess(statusCode, headers, response);
			try {
//				Log.i("zq",response.toString());
				boolean code = response.getBoolean("code");
				String msg = response.getString("msg");
				if (code == true) {
					JSONObject object = response.getJSONObject("data");
					JSONObject object2 = object.getJSONObject("order");
						String order_time = object2.getString("order_time");
//						Log.i("zq", order_time);
						String receive_time = object2.getString("receive_time");
						String total = object2.getString("total");
						tv_xiadan.setText(order_time);
						tv_songhuo.setText(receive_time);
						tv_total.setText(total);
				}else {
					Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
				}
				
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public class JsonShangpin extends JsonHttpResponseHandler {
		@Override
		public void onSuccess(int statusCode, Header[] headers,JSONObject response) {
			super.onSuccess(statusCode, headers, response);
			try {
				//Log.i("zq",response.toString());
				boolean code = response.getBoolean("code");
				String msg = response.getString("msg");
				if (code == true) {
					JSONObject object = response.getJSONObject("data");
					JSONArray array = object.getJSONArray("orderdetails");
						for (int j = 0;j < array.length(); j++) {					
							JSONObject orderdetails = array.getJSONObject(j);
							String name = orderdetails.getString("name");
							String amount = orderdetails.getString("amount");
							String price = orderdetails.getString("price");
							String unit = orderdetails.getString("unit");	
							geshu +=Integer.parseInt(amount);							
							infolist.add(new XiangqingEntity(name, amount, unit, price));
						}
						int shuliang = array.length();
						tv_shangpin.setText(""+shuliang);
						tv_shuliang.setText(""+geshu);
					xiangqingadapter=new XiangqingAdapter(getApplicationContext(), infolist,getIntent().getStringExtra("id"));
					lv.setAdapter(xiangqingadapter);
				} else {
					Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.detaile, menu);
		return true;
	}

}
